package DAO;

import DTO.UserDTO;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Hai
 */
public class ConnectDB extends AbConnectDB {

    private Connection connect = null;
    private Statement statement = null;
    private ResultSet result = null;

    ConnectDB(String host, String user, String pass, String db) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    @Override
    public Connection getConnect() {
        String Host = "";
        String UserName = "root";
        String Password = "";
        String DataBase = "travel";

        String url = "jdbc:mysql://localhost/" + DataBase + "?user=" + UserName + "&password=" + Password + "&useUnicode=yes&characterEncoding=UTF-8";
        String Driver = "com.mysql.jdbc.Driver";

        try {
            // tao ket noi CSDL
            Class.forName(Driver);
            connect = (Connection) DriverManager.getConnection(url);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (connect == null) {
            JOptionPane.showMessageDialog(null, 1);
        }
        return connect;
    }

    @Override
    public Statement getStatement() throws SQLException {
        if (this.statement == null ? true : this.statement.isClosed()) {
            this.statement = this.getConnect().createStatement();
        }
        return this.statement;
    }

    @Override
    public ResultSet executeQuery(String Query) {
        try {
            this.result = getStatement().executeQuery(Query);

        } catch (SQLException ex) {
            Logger.getLogger("Error:" + ex.getMessage() + "-" + Query);
        }
        return this.result;
    }

    @Override
    public int executeUpdate(String Query) throws SQLException {
        int res = Integer.MIN_VALUE;

        try {
            res = getStatement().executeUpdate(Query);
        } catch (SQLException ex) {
            Logger.getLogger("Error:" + ex.getMessage() + "-" + Query);
        } finally {
            this.Close();
        }

        return res;
    }

    @Override
    public UserDTO login(String username, String password) {
        if (username.contains("FASmile") && password.contains("123abc")) {
            UserDTO retUser = new UserDTO(username, password, "FASmile1404@gmail.com", "01669558099", "Lương Thế Hai");
            return retUser;
        }

        return null;
    }

    @Override
    public void Close() throws SQLException {
        if (this.result != null && this.result.isClosed()) {
            this.result.close();
            this.result = null;
        }
        if (this.statement != null && !this.statement.isClosed()) {
            this.statement.close();
            this.statement = null;
        }
        if (this.connect != null && !this.connect.isClosed()) {
            this.connect.close();
            this.connect = null;
        }
    }

}
